package hk.hku.hooku.util;

import hk.hku.hooku.db.EventDB;
import hk.hku.hooku.pojo.Event;
import hk.hku.hooku.pojo.EventChoice;
import hk.hku.hooku.pojo.EventResult;

import java.util.ArrayList;

public class HtmlGenerator {
	public static String toHtml(String url) {
		Event event = EventDB.getFullEventByUrl(url);
		StringBuilder content = new StringBuilder();
		content.append("<p>Dear user:</p>");
		content.append(String
				.format("<p>The result of the arrangement \"%s\" created by \"%s\" has been decided.</p>",
						event.getTitle(), event.getProposerEmail()));
		if(event.getLocation()!=null&&!event.getLocation().equals("")){
			content.append(String.format("<p>The location of the arragement is: %s</p>",
					event.getLocation()));
		}
		content.append(String.format("The decided time:<p>"));
		EventResult result=event.getEventResult();
		ArrayList<EventChoice> choices=result.getChoices();
		for(EventChoice choice:choices){
			if(choice.getStartTime().equals(choice.getEndTime())){
				content.append(String.format("%s<p>",choice.getStartTime()));
			}else{
				content.append(String.format("%s ~ %s<p>",choice.getStartTime(),choice.getEndTime()));
			}
		}
		
		if(result.getNote()!=null&&result.getNote()!=""){
			content.append(String.format("<p>Note of the decision:%s</p>", result.getNote()));
		}
		
		if (event.getDescription() != null
				&& !event.getDescription().equals("")){
			content.append(String.format("<p>The detail:<p> %s<p>", event.getDescription()));
		}
		
		content.append(String.format("<p>For detailed information, please click: <a href=\"http://%s/%s?url=%s\">View Result</a>",Config.HOST,"Hooku/eventVote.jsp",url));
		
		content.append(String.format("<p>Regards,</p>"));
		content.append(String.format("<p>Hooku</p>"));
		content.append(String.format("<p>Hooku is an fast and convenient arrangement system developed by The University of Hong Kong."));
		return content.toString();
	}
	
	public static String toPollHtml(String url) {
		Event event = EventDB.getFullEventByUrl(url);
		StringBuilder content = new StringBuilder();
		content.append("<p>Dear user:</p>");
		content.append(String
				.format("<p>%s has created an arrangement via Hooku for you to poll.",event.getProposerEmail()));
		content.append(String.format("<p>Arrangement Title:%s",event.getTitle()));
		if(event.getLocation()!=null&&!event.getLocation().equals("")){
			content.append(String.format("<p>The location of the arragement is: %s</p>",
					event.getLocation()));
		}
		content.append(String
				.format("<p>Description: %s",event.getDescription()));
		
		content.append(String.format("<p>The polling link: <a href=\"http://%s/%s?url=%s\">View Result</a>",Config.HOST,"Hooku/eventVote.jsp",url));
		
		content.append(String.format("<p>Regards,</p>"));
		content.append(String.format("<p>Hooku</p>"));
		content.append(String.format("<p>Hooku is an fast and convenient arrangement system developed by The University of Hong Kong."));
		return content.toString();
	}
}
